#import "NoteTree.h"

@implementation NoteTree

@synthesize wholeNode;
@synthesize half1Node;
@synthesize half2Node;
@synthesize quarter1Node;
@synthesize quarter2Node;
@synthesize quarter3Node;
@synthesize quarter4Node;
@synthesize eighth1Node;
@synthesize eighth2Node;
@synthesize eighth3Node;
@synthesize eighth4Node;
@synthesize eighth5Node;
@synthesize eighth6Node;
@synthesize eighth7Node;
@synthesize eighth8Node;

-(id)init {
	if (self = [super init]){
		wholeNode = [[NoteNode alloc] initWholeNode];
		
		half1Node = [[NoteNode alloc] initHalfNode:wholeNode];
		half2Node = [[NoteNode alloc] initHalfNode:wholeNode];
		
		quarter1Node = [[NoteNode alloc] initQuarterNode:half1Node];
		quarter2Node = [[NoteNode alloc] initQuarterNode:half1Node];
		
		quarter3Node = [[NoteNode alloc] initQuarterNode:half2Node];
		quarter4Node = [[NoteNode alloc] initQuarterNode:half2Node];
		
		eighth1Node = [[NoteNode alloc] initEighthNode:quarter1Node];
		eighth2Node = [[NoteNode alloc] initEighthNode:quarter1Node];
		
		eighth3Node = [[NoteNode alloc] initEighthNode:quarter2Node];
		eighth4Node = [[NoteNode alloc] initEighthNode:quarter2Node];
		
		eighth5Node = [[NoteNode alloc] initEighthNode:quarter3Node];
		eighth6Node = [[NoteNode alloc] initEighthNode:quarter3Node];
		
		eighth7Node = [[NoteNode alloc] initEighthNode:quarter4Node];
		eighth8Node = [[NoteNode alloc] initEighthNode:quarter4Node];
		
		[wholeNode addChild:half1Node];
		[wholeNode addChild:half2Node];
		
		[half1Node addChild:quarter1Node];
		[half1Node addChild:quarter2Node];
		
		[half2Node addChild:quarter3Node];
		[half2Node addChild:quarter4Node];
		
		[quarter1Node addChild:eighth1Node];
		[quarter1Node addChild:eighth2Node];
		
		[quarter2Node addChild:eighth3Node];
		[quarter2Node addChild:eighth4Node];
		
		[quarter3Node addChild:eighth5Node];
		[quarter3Node addChild:eighth6Node];
		
		[quarter4Node addChild:eighth7Node];
		[quarter4Node addChild:eighth8Node];
		
		NSLog(@"New NoteTree initialized.");
	}
	return self;
}

- (void)clearTree {
	[wholeNode clearAllChildren];
}


-(id)initWithCoder:(NSCoder*)coder {
	wholeNode = [[coder decodeObjectForKey:@"wholeNode"] retain];
	half1Node = [[coder decodeObjectForKey:@"half1Node"] retain];
	half2Node = [[coder decodeObjectForKey:@"half2Node"] retain];
	quarter1Node = [[coder decodeObjectForKey:@"quarter1Node"] retain];
	quarter2Node = [[coder decodeObjectForKey:@"quarter2Node"] retain];
	quarter3Node = [[coder decodeObjectForKey:@"quarter3Node"] retain];
	quarter4Node = [[coder decodeObjectForKey:@"quarter4Node"] retain];
	eighth1Node = [[coder decodeObjectForKey:@"eighth1Node"] retain];
	eighth2Node = [[coder decodeObjectForKey:@"eighth2Node"] retain];
	eighth3Node = [[coder decodeObjectForKey:@"eighth3Node"] retain];
	eighth4Node = [[coder decodeObjectForKey:@"eighth4Node"] retain];
	eighth5Node = [[coder decodeObjectForKey:@"eighth5Node"] retain];
	eighth6Node = [[coder decodeObjectForKey:@"eighth6Node"] retain];
	eighth7Node = [[coder decodeObjectForKey:@"eighth7Node"] retain];
	eighth8Node = [[coder decodeObjectForKey:@"eighth8Node"] retain];
	
	[wholeNode addChild:half1Node];
	[wholeNode addChild:half2Node];
	
	[half1Node addChild:quarter1Node];
	[half1Node addChild:quarter2Node];
	
	[half2Node addChild:quarter3Node];
	[half2Node addChild:quarter4Node];
	
	[quarter1Node addChild:eighth1Node];
	[quarter1Node addChild:eighth2Node];
	
	[quarter2Node addChild:eighth3Node];
	[quarter2Node addChild:eighth4Node];
	
	[quarter3Node addChild:eighth5Node];
	[quarter3Node addChild:eighth6Node];
	
	[quarter4Node addChild:eighth7Node];
	[quarter4Node addChild:eighth8Node];
	return self;
}

-(void)encodeWithCoder:(NSCoder*)encoder {
	[encoder encodeObject: wholeNode forKey: @"wholeNode"];
	[encoder encodeObject: half1Node forKey: @"half1Node"];
	[encoder encodeObject: half2Node forKey: @"half2Node"];
	[encoder encodeObject: quarter1Node forKey: @"quarter1Node"];
	[encoder encodeObject: quarter2Node forKey: @"quarter2Node"];
	[encoder encodeObject: quarter3Node forKey: @"quarter3Node"];
	[encoder encodeObject: quarter4Node forKey: @"quarter4Node"];
	[encoder encodeObject: eighth1Node forKey: @"eighth1Node"];
	[encoder encodeObject: eighth2Node forKey: @"eighth2Node"];
	[encoder encodeObject: eighth3Node forKey: @"eighth3Node"];
	[encoder encodeObject: eighth4Node forKey: @"eighth4Node"];
	[encoder encodeObject: eighth5Node forKey: @"eighth5Node"];
	[encoder encodeObject: eighth6Node forKey: @"eighth6Node"];
	[encoder encodeObject: eighth7Node forKey: @"eighth7Node"];
	[encoder encodeObject: eighth8Node forKey: @"eighth8Node"];	
}


@end
